package cn.iocoder.yudao.module.wine.controller.admin.businesscredit;

import cn.iocoder.yudao.module.wine.convert.business.BusinessCreditConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.businesscredit.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.businesscredit.BusinessCreditDO;
import cn.iocoder.yudao.module.wine.service.businesscredit.BusinessCreditService;

@Tag(name = "管理后台 - 会员等级管理")
@RestController
@RequestMapping("/wine/business-credit")
@Validated
public class BusinessCreditController {

    @Resource
    private BusinessCreditService businessCreditService;

    @PostMapping("/create")
    @Operation(summary = "创建会员等级管理")
    @PreAuthorize("@ss.hasPermission('wine:business-credit:create')")
    public CommonResult<Long> createBusinessCredit(@Valid @RequestBody BusinessCreditSaveReqVO createReqVO) {
        return success(businessCreditService.createBusinessCredit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员等级管理")
    @PreAuthorize("@ss.hasPermission('wine:business-credit:update')")
    public CommonResult<Boolean> updateBusinessCredit(@Valid @RequestBody BusinessCreditSaveReqVO updateReqVO) {
        businessCreditService.updateBusinessCredit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员等级管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:business-credit:delete')")
    public CommonResult<Boolean> deleteBusinessCredit(@RequestParam("id") Long id) {
        businessCreditService.deleteBusinessCredit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员等级管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:business-credit:query')")
    public CommonResult<BusinessCreditRespVO> getBusinessCredit(@RequestParam("id") Long id) {
        BusinessCreditDO businessCredit = businessCreditService.getBusinessCredit(id);
        //BeanUtils.toBean(businessCredit, BusinessCreditRespVO.class)
        return success(BusinessCreditConvert.INSTANCE.convert(businessCredit));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员等级管理分页")
    @PreAuthorize("@ss.hasPermission('wine:business-credit:query')")
    public CommonResult<PageResult<BusinessCreditRespVO>> getBusinessCreditPage(@Valid BusinessCreditPageReqVO pageReqVO) {
        PageResult<BusinessCreditDO> pageResult = businessCreditService.getBusinessCreditPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessCreditRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员等级列表")
    public CommonResult<List<BusinessCreditRespVO>> getBusinessCreditList() {
        List<BusinessCreditDO> list = businessCreditService.getBusinessCreditList();
        return success(BeanUtils.toBean(list, BusinessCreditRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出会员等级管理 Excel")
    @PreAuthorize("@ss.hasPermission('wine:business-credit:export')")
    @OperateLog(type = EXPORT)
    public void exportBusinessCreditExcel(@Valid BusinessCreditPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessCreditDO> list = businessCreditService.getBusinessCreditPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员等级管理.xls", "数据", BusinessCreditRespVO.class,
                        BeanUtils.toBean(list, BusinessCreditRespVO.class));
    }

}
